import threading

from bs4 import BeautifulSoup
from util import utils
path = "src/img"

#<img class="image lazy-loaded" src="https://images.sex.com/images/pinporn/2018/10/16/300/20092816.jpg" data-src="https://images.sex.com/images/pinporn/2018/10/16/300/20092816.jpg" width="300" height="420" alt="The only Kana Momonogi">
# 获取网页中的照片列表
def getAllImg(html):
    list = []
    t1 = BeautifulSoup(html, 'html.parser').find_all('img')
    for img in t1:
        if str(img).__contains__('data-src'):
            list.append(img.get('data-src'))
    return list

def start(pageIndex):
    if pageIndex == 1:
        download("https://www.sex.com/pics/")
    else:
        download("https://www.sex.com/pics/?page=" + str(pageIndex))


def download(url):
    html = utils.getHtmlUtf_8(url)
    imgList = getAllImg(html)
    print(imgList)
    utils.saveImages(imgList,path)

n = 1

def loop():
    start(n)

if __name__ == "__main__":
    while 1 <= n < 31:
        t = threading.Thread(target=loop, name='LoopThread1')
        t.start()
        n = n + 1